package com.sduept.nwld.fault.outer.model.factory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.sduept.bigdata.fault.common.ParamDesc;
import com.sduept.bigdata.fault.entity.FaultRecord;
import com.sduept.bigdata.weather.manager.FaultWeatherInfoManager;
import com.sduept.bigdata.weather.manager.WeatherManager;
import com.sduept.cim.query.service.LineTowerQueryService;
import com.sduept.cim.query.service.impl.LineTowerQueryServiceImpl;
import com.sduept.core.ParamManager;
import com.sduept.core.dao.SuperDAO;
import com.sduept.core.entity.SystemParameter;
import com.sduept.nwld.dataserver.manager.cim.CimManager;
import com.sduept.nwld.fault.outer.model.FaultTripInfo;
import com.sduept.nwld.fault.outer.model.ProtectInfo;
import com.sduept.utils.DateUtil;
import com.sduept.utils.SpringContext;
import com.sduept.utils.WebRequestUtil;
import org.apache.commons.lang.StringUtils;
import org.jsoup.Connection.Method;

import java.math.BigDecimal;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


public abstract class AbstractOuterFaultModelFactory {
	protected CimManager cm = SpringContext.getBean(CimManager.class);
	protected SuperDAO superDao = SpringContext.getBean(SuperDAO.class);
	protected LineTowerQueryService fam = SpringContext.getBean(LineTowerQueryServiceImpl.class);
	protected FaultWeatherInfoManager wdm  = SpringContext.getBean(FaultWeatherInfoManager.class);
	protected WeatherManager wm  = SpringContext.getBean(WeatherManager.class);
	
	public abstract FaultTripInfo getFaultTripInfo(FaultRecord record)throws Exception;
	/**
	 * 获得指定精度的浮点数
	 * @param precision
	 * @param value
	 * @return
	 */
	protected double getNeededPrecisionNumber(int precision,double value){
		BigDecimal b = new BigDecimal(value); 
		double df = b.setScale(precision, BigDecimal.ROUND_HALF_UP).doubleValue();
		return df;
	}
	/**
	 * 获得指定精度的浮点数
	 * @param precision
	 * @param value
	 * @return
	 */
	protected double getNeededPrecisionNumber(double value){
		return getNeededPrecisionNumber(3,value);
	}
	/**
	 * 获得四方保信保护动作报告
	 * @param faultTime
	 * @param deviceName
	 * @param stationName
	 * @return
	 */
	protected List<ProtectInfo> getSFProtectReport(Date faultTime,String deviceName,String stationName){
		SystemParameter osbIpSp = SpringContext.getBean(ParamManager.class).get(ParamDesc.OSB_IP);
		Map<String,String> requestParams = new HashMap<>();
		requestParams.put("faultTime", DateUtil.getStringDate(faultTime, DateUtil.pattern_ymdhms));
		requestParams.put("equipmentName",deviceName);
		requestParams.put("stationName", stationName);
		List<ProtectInfo> result = new ArrayList<ProtectInfo>();
		try {
			 String jsonStr = WebRequestUtil.getRequest("http://"+osbIpSp.getValue()+"/rest/EfileQueryService/getBayRelatedDisconnectorStatus", Method.GET, requestParams);
			 if(StringUtils.isNotEmpty(jsonStr)){
				JSONArray array = JSONArray.parseArray(jsonStr);
				result = JSONObject.parseArray(array.toJSONString(), ProtectInfo.class);
				for(ProtectInfo info:result){
					//设置调整后的动作元件信息
					String comInfo  = info.getComponet();
					Pattern p = Pattern.compile("[0-9]{1,}ms");
					Matcher m = p.matcher(comInfo);
					if(m.find()){
						int index = comInfo.indexOf(m.group(0));
						String newcomInfo = comInfo.substring(index);
						info.setComponet(newcomInfo);
					}
					//抽取测距信息
					p = Pattern.compile("测距(:|：)[0-9]{1,}(.)?[0-9]{1,}(k|K)(m|M)");
					m = p.matcher(comInfo);
					if(m.find()){
						String str = m.group(0);
						p = Pattern.compile("[0-9]{1,}(.)?[0-9]{1,}");
						m = p.matcher(str);
						if(m.find()){
							info.setDistance(Double.parseDouble(m.group(0)));
						}
					}
				}
			}
		} catch (Exception e) {
			//e.printStackTrace();
		}
		return result;
	}
}
